package com.llmcu.source.javase.objectoriented.internalclass;

import lombok.Data;

/**
 * 匿名内部类
 *
 * @author liuling
 * @date 2024/10/6 12:50
 */
public class AnonymousInternalClass {
    static int year = 10;

    public static void main(String[] args) throws InterruptedException {
        final int[] age = {10};
        final int age0 = 10;
        final Student student = new AnonymousInternalClass().new Student();
        Thread thread = new Thread() {
            @Override
            public void run() {
                age[0]++;
//                age0++;
                student.setAge(student.getAge() + 1);
            }
        };
        thread.start();
        thread.join();
        System.out.println("age:" + age[0]);
        System.out.println("stuent.age:" + student.getAge());

        Thread thread1 = new Thread() {
            @Override
            public void run() {
                // 成员变量不用 final
                year++;
            }
        };
        thread1.start();
        thread1.join();
        System.out.println("year:" + year);

        class MyThread extends Thread {
            @Override
            public void run() {
                // 内部类不用 final
                year++;
            }
        }
        MyThread myThread = new MyThread();
        myThread.start();
        myThread.join();
        System.out.println("year:" + year);


    }

    @Data
    class Student {
        private int age;
    }
}
